<?php

/**
 *  @author  Lynn Cho <lynncho@live.com>
 *  @createTime  2016-9-5 16:49:34
 *  @copyright  shewen technology
 *  @descrition  
 */
class BaseController extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('session');
        self::needLogin();
    }

    /**
     * dose current login user has a specify permission
     * @param int $prvid function id accroding to database
     * @return boolean
     */
    public function hasPermission($prvid) {
        $id = $this->session->manager->id;
        if (is_numeric($id) && is_numeric($prvid)) {
            $permission = $this->db
                    ->where('mid', $id)
                    ->where('prvid', $prvid)
                    ->select('mid')
                    ->get('funcprv')
                    ->row_array();
            if (empty($permission)) {
                return false;
            } else {
                return true;
            }
        }
    }

    /**
     * need login to manage
     */
    private function needLogin() {
        if (!$this->session->has_userdata('manager')) {
            echo "<script>alert('请先登录！');location='" . $this->config->item('base_url') . "login';</script>";
        }
    }

}
